//
//  ZPMineTiaoKeViewController.m
//  ZPIOSEducation
//
//  Created by 董伟1 on 17/2/24.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPMineTiaoKeViewController.h"
#import "ZPSelectCurriculumCell.h"
#import "ZPSelectClassVC.h"

@interface ZPMineTiaoKeViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray  *selectArr;
}

@property (weak, nonatomic) IBOutlet UILabel *classNameLab;
@property (weak, nonatomic) IBOutlet UILabel *dateLab;
@property (weak, nonatomic) IBOutlet UILabel *statusLab;
@property (weak, nonatomic) IBOutlet UILabel *teacherNameLab;
@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;

@end

@implementation ZPMineTiaoKeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"调课";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(navLeftButtonClick)];
    self.nextBtn.layer.cornerRadius = 4.;
    self.nextBtn.layer.masksToBounds = YES;
    self.nextBtn.userInteractionEnabled = NO;
    
    selectArr = [[NSMutableArray  alloc] init];
    [self.mainCollectionView  registerNib:[UINib  nibWithNibName:@"ZPSelectCurriculumCell" bundle:nil] forCellWithReuseIdentifier:@"ZPSelectCurriculumCell"];
    
    self.classNameLab.text = [NSString  stringWithFormat:@"%@",[self.dataDic  objectForKey:@"className"]];
    self.dateLab.text = [NSString  stringWithFormat:@"%@",[self.dataDic  objectForKey:@"time"]];
    self.statusLab.text = [NSString  stringWithFormat:@"已完成 %@",[self.dataDic  objectForKey:@"finish"]];
    self.teacherNameLab.text = [NSString  stringWithFormat:@"%@    %@",[self.dataDic  objectForKey:@"teachName"],[self.dataDic  objectForKey:@"teacherName"]];
    
    NSInteger  count = [[self.dataDic  objectForKey:@"scheduleList"] count];
    if (count > 0) {
        if (count % 6 == 0) {
            self.viewHeight.constant = 48 * (count / 6) + 28 + 20;
        }else {
            self.viewHeight.constant = 48 * ((count / 6) + 1) + 28 + 20;
        }
    }else {
        self.viewHeight.constant = 0.;
    }
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.scrollEnabled = NO;
    
}

- (void)navLeftButtonClick
{
    [self.navigationController  popViewControllerAnimated:YES];
}

- (IBAction)nextButtonClick:(id)sender
{
    ZPSelectClassVC  *viewController = [[ZPSelectClassVC  alloc] initWithNibName:@"ZPSelectClassVC" bundle:nil];
    viewController.classId = self.classId;
    viewController.chooseListStr = [NSString  stringWithFormat:@"%@",[selectArr  componentsJoinedByString:@","]];
    [self.navigationController  pushViewController:viewController animated:YES];
}

#pragma mark - UICollectionView Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.dataDic  objectForKey:@"scheduleList"]  count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREENWIDTH - 6 - 16 - 20 - 40) / 6 - 0.1, 48);
}

- (UICollectionViewCell  *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"ZPSelectCurriculumCell";
    ZPSelectCurriculumCell  *cell = (ZPSelectCurriculumCell  *)[collectionView  dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    NSInteger  row = [indexPath  row];
    NSDictionary  *tempDic = [[self.dataDic  objectForKey:@"scheduleList"] objectAtIndex:row];
    cell.date.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"date"]];
    cell.numberLab.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"sort"]];
    if ([[tempDic  objectForKey:@"canChoose"] integerValue] == 0) {
        cell.numberLab.textColor = RGBACOLOR(153, 153, 153, 1.);
        cell.userInteractionEnabled = NO;
    }else {
        cell.numberLab.textColor = RGBACOLOR(18, 95, 137, 1.);
        cell.userInteractionEnabled = YES;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger  row = [indexPath  row];
    ZPSelectCurriculumCell  *cell = (ZPSelectCurriculumCell  *)[collectionView  cellForItemAtIndexPath:indexPath];
    NSDictionary  *tempDic = [[self.dataDic  objectForKey:@"scheduleList"] objectAtIndex:row];
    NSString  *currentIdStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"id"]];
    if ([selectArr  containsObject:currentIdStr]) {
        [selectArr  removeObject:currentIdStr];
        cell.numberLab.backgroundColor = [UIColor  whiteColor];
    }else {
        [selectArr  addObject:currentIdStr];
        cell.numberLab.backgroundColor = [UIColor  grayColor];
    }
    
    if (selectArr.count > 0) {
        [self.nextBtn  setBackgroundColor:RGBACOLOR(33, 164, 235, 1.)];
        self.nextBtn.userInteractionEnabled = YES;
    }else {
        [self.nextBtn  setBackgroundColor:RGBACOLOR(204, 204, 204, 1.)];
        self.nextBtn.userInteractionEnabled = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
